<?php
/**
 * 周年庆盲盒获得配置
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AnniversaryUserController extends BaseController
{
    /**
     * @api {Get} /anniversaryuser/index 查看标记用户列表
     * @apiGroup 补充接口-盲盒管理
     *
     * @apiParam {Int} user_code 用户编号
     * @apiParam {Int} sold 是否充值：0：不限制；1：是；2：否
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "list": [
    {
    "id": 7,
    "add_time": 0,
    "user_code": 903828,
    "nickname": "司马沛柔903828",
    "total_recharge": "0.02",
    "offline_time": "在线",
    "blind_box_number": 0,
    "blind_box_prize": "300.00",
    "total_recharge_act": 0
    },
    {
    "id": 3,
    "add_time": 0,
    "user_code": 724926,
    "nickname": "564854",
    "total_recharge": "88.00",
    "offline_time": "2021-04-27 14:12:25",
    "blind_box_number": "133",
    "blind_box_prize": 0,
    "total_recharge_act": 0
    },
    {
    "id": 2,
    "add_time": 0,
    "user_code": 950818,
    "nickname": "左耳用户950818",
    "total_recharge": "388.00",
    "offline_time": "2020-10-21 13:08:53",
    "blind_box_number": 0,
    "blind_box_prize": 0,
    "total_recharge_act": 0
    },
    {
    "id": 1,
    "add_time": 0,
    "user_code": 617040,
    "nickname": "提莫队长呢",
    "total_recharge": "0.07",
    "offline_time": "在线",
    "blind_box_number": "21",
    "blind_box_prize": 0,
    "total_recharge_act": 0
    }
    ],
    "total_blind_box_user": 1,
    "total_blind_box_number": "14",
    "total_blind_box_prize": 300,
    "total_recharge": 0.02
    }
    }
     */
    public function index(Request $request)
    {
        $user_code = $request->user_code;
        $sold = $request->sold;

        $actConfig = app('model_config')->where('key', 'ACTIVITY_ANNIVERSARY_CONFIG')->value('value');
        $actConfig = json_decode($actConfig, true);
        $start_time = strtotime($actConfig['start_time']);
        $end_time = strtotime($actConfig['end_time']);
        $where_act_time = [$start_time, $end_time];

        if ($user_code) {
            $user_id_search = app('model_user')->where(['user_no' => $user_code])->where('deleted', 0)->value('user_id');
            $where['user_id'] = $user_id_search;
        }

        $search_user_ids = [0];
        if (in_array($sold, [1, 2])) {
            //上线后充值的用户id
            $userRechargeRecords = app('model_user_recharge')
                ->select('user_id')
                ->where(['status' => 2])
                ->whereBetween('add_time', $where_act_time)
                ->groupBy('user_id')
                ->get();

            foreach ($userRechargeRecords as $userRechargeRecord) {
                $search_user_ids[] = $userRechargeRecord->user_id;
            }
        }

        $list = app('model_anniversary_user')
            ->where(function ($query) use ($sold, $search_user_ids) {
                if ($search_user_ids) {
                    if ($sold == 1) {
                        $query->whereIn('user_id', $search_user_ids);
                    } else {
                        $query->whereNotIn('user_id', $search_user_ids);
                    }
                }
            })
            ->orderBy('id', 'desc')
            ->paginate(20)->toArray();
        if ($list) {
            $user_ids = array_column($list['data'], 'user_id');
            $user_data_array = app('model_user_data')->from('ln_user_data as ud')
                ->join('ln_user as u', 'ud.user_id', '=', 'u.user_id')
                ->select('ud.user_id', 'ud.nickname', 'ud.head_picture', 'ud.user_code', 'u.offline_time')
                ->whereIn('ud.user_id', $user_ids)
                ->get()->toArray();
            //用户信息
            $user_data = array_column($user_data_array, null, 'user_id');

            //历史充值信息
            $recharge_array = app('model_user_recharge')
                ->select(DB::raw('sum(money) as total, user_id'))
                ->whereIn('user_id', $user_ids)
                ->where('status', 2)
                ->groupBy('user_id')
                ->get()->toArray();
            $recharge_data = array_column($recharge_array, null, 'user_id');

            //领取盲盒个数信息
            $blind_box_number_array = app('model_anniversary_log')
                ->select(DB::raw('sum(blind_box_number) as total, user_id'))
                ->whereIn('user_id', $user_ids)
                ->where('user_type', 2)
                ->groupBy('user_id')
                ->get()->toArray();
            $blind_box_number_data = array_column($blind_box_number_array, null, 'user_id');

            //背包盲盒开出的礼物信息
            $blind_box_prize_array = app('model_gift_box_prize_detail')
                ->select(DB::raw('sum(gold_coin) as total, user_id'))
                ->whereIn('user_id', $user_ids)
                ->where('source', 1)
                ->whereBetween('add_time', $where_act_time)
                ->groupBy('user_id')
                ->get()->toArray();
            $blind_box_prize_data = array_column($blind_box_prize_array, null, 'user_id');

            //上线后充值记录
            $recharge_array_act = app('model_user_recharge')
                ->select(DB::raw('sum(money) as total,user_id'))
                ->where('user_id', $user_ids)
                ->where('status', 2)
                ->whereBetween('add_time', $where_act_time)
                ->groupBy('user_id')
                ->get()->toArray();
            $recharge_data_act = array_column($recharge_array_act, null, 'user_id');
        }
        $return_list = [];
        foreach ($list['data'] as $key => $item) {
            if (isset($user_data[$item['user_id']]['offline_time'])) {
                if ($user_data[$item['user_id']]['offline_time'] == 0) {
                    $offline_msg = '在线';
                } else {
                    $offline_msg = date('Y-m-d H:i:s', $user_data[$item['user_id']]['offline_time']);
                }
            } else {
                $offline_msg = '-';
            }
            $return_list[] = [
                'id' => $item['id'],
                'add_time' => $item['add_time'],
                'user_code' => $user_data[$item['user_id']]['user_code'] ?? '',
                'nickname' => $user_data[$item['user_id']]['nickname'] ?? '',
                'total_recharge' => $recharge_data[$item['user_id']]['total'] ?? 0,
                'offline_time' => $offline_msg,
                'blind_box_number' => $blind_box_number_data[$item['user_id']]['total'] ?? 0,
                'blind_box_prize' => $blind_box_prize_data[$item['user_id']]['total'] ?? 0,
                'total_recharge_act' => $recharge_data_act[$item['user_id']]['total'] ?? 0,
            ];
        }

        $all_act_user = app('model_anniversary_user')->get()->toArray();
        $all_act_user_array = array_column($all_act_user, 'user_id', 'user_id');

        //累计领取盲盒用户人数
        $total_blind_box_user = app('model_anniversary_log')
            ->whereBetween('add_time', $where_act_time)
            ->where('blind_box_number', '>', 0)
            ->where('user_type', 2)
            ->count(DB::raw('distinct user_id'));
        //累计领取盲盒数量
        $total_blind_box_number = app('model_anniversary_log')
            ->whereBetween('add_time', $where_act_time)
            ->where('user_type', 2)
            ->sum('blind_box_number');
        //累计开出盲盒价值
        $total_blind_box_prize_list = app('model_gift_box_prize_detail')
            ->where(['source' => 1])
            ->whereBetween('add_time', $where_act_time)
            ->get()->toArray();
        $total_blind_box_prize = 0;
        foreach ($total_blind_box_prize_list as $key => $value) {
            if (isset($all_act_user_array[$value['user_id']])) {
                $total_blind_box_prize += $value['gold_coin'];
            }
        }

        //上线后总充值金额
        $total_recharge_list = app('model_user_recharge')
            ->select('user_id', 'money')
            ->whereBetween('add_time', $where_act_time)
            ->where(['status' => 2])
            ->get()->toArray();
        $total_recharge = 0;
        foreach ($total_recharge_list as $key => $value) {
            if (isset($all_act_user_array[$value['user_id']])) {
                $total_recharge += $value['money'];
            }
        }

        return Response::sendData([
                'list' => $return_list,
                'total_blind_box_user' => $total_blind_box_user,
                'total_blind_box_number' => $total_blind_box_number,
                'total_blind_box_prize' => $total_blind_box_prize,
                'total_recharge' => $total_recharge,
            ]);
    }
}
